package cn.edu.dgut.css.sai.jpa.springdatajpaspecifications.controller;

import cn.edu.dgut.css.sai.jpa.springdatajpaspecifications.model.Employee;
import cn.edu.dgut.css.sai.jpa.springdatajpaspecifications.repository.EmployeeRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.edu.dgut.css.sai.jpa.springdatajpaspecifications.specifications.EmployeeSpecification.*;
import static org.springframework.data.jpa.domain.Specification.where;

/**
 * @author sai
 * @since 2020-12-13
 */
@RestController
@RequiredArgsConstructor
public class EmployeeController {
    private final EmployeeRepository employeeRepository;

    @PostMapping("/employees")
    public Employee createEmployee(@RequestBody Employee employee) {
        return employeeRepository.save(employee);
    }

    @GetMapping("/employees")
    public List<Employee> findAllEmployees() {
        return employeeRepository.findAll();
    }

    @GetMapping("/employees/{firstname}/{department}")
    public List<Employee> findByFirstnameAndDepartment(@PathVariable("firstname") String firstname,
                                                       @PathVariable("department") String department) {
        return employeeRepository.findAll(where(hasFirstName(firstname).and(hasDepartment(department))));
    }

    @GetMapping("/employees/{lastname}")
    public List<Employee> findByFirstnameAndDepartment(@PathVariable("lastname") String lastname) {
        return employeeRepository.findAll(containsLastName(lastname));
    }


}
